<template>
  <div class="admin-login-container">
    <div class="login-background"></div>
    <div class="login-card">
      <div class="login-header">
        <img src="@/assets/LoginImg/jng2.png" alt="Logo" class="login-logo" />
        <h2 class="login-title">钓鱼助手管理系统</h2>
        <p class="login-subtitle">Management System</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入管理员账号"
            size="large"
            :prefix-icon="User"
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            :prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登 录' }}
          </el-button>
        </el-form-item>

        <el-form-item>
          <el-button
            type="info"
            size="large"
            :loading="backLoading"
            class="back-button"
            @click="handleBackToUserLogin"
          >
            {{ backLoading ? '跳转中...' : '返回用户登录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <p>© 2025 钓鱼助手管理系统. All Rights Reserved.</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import request from '@/utils/request'
import { useAuthStore } from '@/stores/authStore'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)
const backLoading = ref(false)
const auth = useAuthStore()

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入管理员账号', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度为3-20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 20, message: '密码长度为5-20个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const response = await request.post('/admin/auth/login', null, {
          params: {
            username: loginForm.username,
            password: loginForm.password
          }
        })

        if (response.code === 0) {
          const token = response.data.token
          // 存储管理员Token
          auth.setAdminToken(token)

          // 拉取管理员信息与权限
          try {
            const infoRes = await request.get('/admin/auth/info')
            if (infoRes.code === 0) {
              const admin = infoRes.data?.admin
              const permissions = infoRes.data?.permissions || []
              auth.setAdminInfo(admin)
              auth.setAdminPermissions(permissions)
            }
          } catch {}
          
          ElMessage.success('登录成功！')
          router.push('/admin/dashboard')
        } else {
          ElMessage.error(response.message || '登录失败')
        }
      } catch (error) {
        console.error('登录失败', error)
        ElMessage.error(error.response?.data?.message || '登录失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}

const handleBackToUserLogin = () => {
  if (loading.value || backLoading.value) return
  backLoading.value = true
  setTimeout(() => {
    router.push('/userLogin')
  }, 1000)
}
</script>

<style scoped>
.admin-login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgb(202, 253, 255);
  z-index: 0;
}

.login-card {
  position: relative;
  z-index: 1;
  width: 420px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-logo {
  width: 80px;
  height: 80px;
  margin-bottom: 16px;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%,
  100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-10px);
  }
}

.login-title {
  font-size: 26px;
  font-weight: 600;
  color: #333;
  margin: 0 0 8px 0;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
  font-weight: 300;
  letter-spacing: 2px;
}

.login-form {
  margin-top: 30px;
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
}

.back-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px solid #e8e8e8;
}

.login-footer p {
  margin: 0;
  font-size: 12px;
  color: #999;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
}
</style>

